package com.ydm.myapp001.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.ydm.myapp001.R;
import com.ydm.myapp001.adapter.StickyGridAdapter;
import com.ydm.myapp001.bean.User;

import java.util.ArrayList;
import java.util.List;

public class Second004Activity extends AppCompatActivity {
    private static final String TAG = "YDMLogSecond003Activity";
    private GridView gView; //GridView控件
    private List<User> list; //存放数据源的集合
    //图片id封装为一个数组
    private int[] images = {R.drawable.test_grid_img001, R.drawable.test_grid_img002, R.drawable.test_grid_img003, R.drawable.test_grid_img004, R.drawable.test_grid_img005, R.drawable.test_grid_img006, R.drawable.test_grid_img007};
    private String[] names = {"用户001", "用户002", "用户003", "用户004", "用户005", "用户006", "用户007"};
    private int[] headIds = {1,1,2,2,2,3,3};
    private String[] groundNames = {"我的同学","我的同学","我的家人","我的家人","我的家人","我的同事","我的同事"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second004);


        gView = (GridView)findViewById(R.id.grid_bysection);

        /*
        * 1.加载数据源，在这里只是模拟一个从数据库中查询数据的操作。（模拟数据库查询数据的操作）
        * */
        list = new ArrayList<User>();
        for(int i=0;i<names.length;i++){
            User u = new User();
            u.setName(names[i]);
            u.setPic(images[i]);
            u.setHeaderId(headIds[i]);
            u.setUserGroundName(groundNames[i]);
            list.add(u);
        }




        /*
        * 2.创建自定义适配器
        * */
        StickyGridAdapter adapter = new StickyGridAdapter(this);
        adapter.setListItems(list);
        gView.setAdapter(adapter);
        gView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d(TAG, "onItemClick: 点击了" + names[i]);
            }
        });
    }
}
